﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;

using Actuals.Business;
using Actuals.Common;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using System.Data;


namespace Actuals
{
    public partial class ShowSprintDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            if (!Page.IsPostBack)
            {
                IBusinessProject businessproject = new BusinessProject();
                List<IProject> projects = businessproject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ddlProject.Items.Insert(0, "Select");
               


            }

            else
            {
                if (Request.Form["__EVENTTARGET"] == "refresh")
                    BindRepeaterView();

            }




            /* IList<ITechnicalTask> TechnicalTasks = new List<ITechnicalTask>();

     
             ITechnicalTask technicalTask1 = new TechnicalTask();
             technicalTask1.TechnicalTaskId = 1;
             technicalTask1.TaskCompletedBy = "Muthu";
             technicalTask1.Status = "1";

             ITechnicalTask technicalTask2 = new TechnicalTask();
             technicalTask2.TechnicalTaskId = 2;
             technicalTask2.TaskCompletedBy = "Muthu";
             technicalTask2.Status = "1";

             ITechnicalTask technicalTask3 = new TechnicalTask();
             technicalTask3.TechnicalTaskId = 3;
             technicalTask3.TaskCompletedBy = "Muthu";
             technicalTask3.Status = "1";



             TechnicalTasks.Add(technicalTask1);
             TechnicalTasks.Add(technicalTask2);
             TechnicalTasks.Add(technicalTask3);
             

             IBusinessUserStory businessUserStory = new BusinessUserStory();
           IUserStory userStory1 = businessUserStory.GetUserStory(1);
           userStory1.TechnicalTasks = TechnicalTasks;

           IBusinessUserStory businessUserStory1 = new BusinessUserStory();
           IUserStory userStory2 = businessUserStory1.GetUserStory(2);
           userStory2.TechnicalTasks = TechnicalTasks; 

             IList<IUserStory> userStories  = new List<IUserStory>();
             userStories.Add(userStory1);
             userStories.Add(userStory2);


             _itemsRepeater.DataSource = userStories;
             _itemsRepeater.DataBind(); */


        }



        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
             ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
           

        }
        protected void ddlProject_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            ddlRelease.Items.Clear();
            FillRelease(ddlProject.SelectedValue);
        }
        protected void FillSprint(string projectId, string releaseId)
        {
            IBusinessSprint businessSprint = new BusinessSprint();
            IList<ISprint> sprints = businessSprint.GetSprints(int.Parse(projectId), int.Parse(releaseId));

            ddlSprint.DataSource = sprints;
            ddlSprint.DataTextField = "SprintDescription";
            ddlSprint.DataValueField = "SprintId";
            ddlSprint.DataBind();
           

        }

        protected void ddlRelease_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillSprint(ddlProject.SelectedValue, ddlRelease.SelectedValue);
        }
        protected void btn_GetSprintDetails_click(object sender, EventArgs e)
        {
            BindRepeaterView();

        }
              

        protected void BindRepeaterView()
        {

            //int sprintId = int.Parse(ddlSprint.SelectedValue);

            ////IBusinessUserStory businessUserStory = 
            // IBusinessTechnicalTask businesstechnicalTask = new BusinessTechnicalTask();
            // IList<ITechnicalTask> technicalTasks = businesstechnicalTask.getTechTaskWithSprintId(sprintId);
            // IBusinessUserStory businessUserStory = new BusinessUserStory();
            // IList<IUserStory> userStories = businesstechnicalTask.GetDistinctUserStory(sprintId);

         

            //foreach (IUserStory userStory in userStories)
            //{
            //    int _userStoryId = userStory.UserStoryId;
            //    //IBusinessTechnicalTask businesstechnicalTasksList = new BusinessTechnicalTask();
            //    IList<ITechnicalTask> technicalTasksList = technicalTasks.Where(x => x.UserStoryId == userStory.UserStoryId).ToList();
            //    userStory.TechnicalTasks = technicalTasksList;
              
            //}

            //repeaterShowUserStoryDetails.DataSource = userStories.OrderBy(x => x.TaskId);
            //repeaterShowUserStoryDetails.DataBind();

            int sprint = int.Parse(ddlSprint.SelectedValue);
            IBusinessUserStory businessUserStory = new BusinessUserStory();
            IList<IUserStory> userStories = businessUserStory.GetUserStoriesWithSprint(sprint);

            foreach (IUserStory userStory in userStories)
            {
                int _userStoryId = userStory.UserStoryId;
                IBusinessTechnicalTask businesstechnicalTask = new BusinessTechnicalTask();
                IList<ITechnicalTask> technicalTasks = businesstechnicalTask.GetTechnicalTasks(_userStoryId);

                IList<ITechnicalTask> technicalTasksToDo = new List<ITechnicalTask>();
                IList<ITechnicalTask> technicalTasksInProg = new List<ITechnicalTask>();
                IList<ITechnicalTask> technicalTasksDone = new List<ITechnicalTask>();

                foreach (ITechnicalTask technicalTask in technicalTasks)
                {
                    if ((technicalTask.Status == "T") || (technicalTask.Status==string.Empty))
                        technicalTasksToDo.Add(technicalTask);
                    else if (technicalTask.Status == "I")
                        technicalTasksInProg.Add(technicalTask);
                    else if (technicalTask.Status == "C")
                        technicalTasksDone.Add(technicalTask);

                }

                userStory.TechnicalTasks = technicalTasks;
                userStory.TechnicalTasksToDo = technicalTasksToDo;
                userStory.TechnicalTasksInProg = technicalTasksInProg;
                userStory.TechnicalTasksDone = technicalTasksDone;

               
            }

            repeaterShowUserStoryDetails.DataSource = userStories;
            repeaterShowUserStoryDetails.DataBind();
        }
        


        protected string  getUserStoryAndTaskShortName(object taskId,object userStoryId)
        {
            IBusinessUserStory businessUserStory = new BusinessUserStory();
            IUserStory userStory = businessUserStory.GetUserStory(int.Parse(userStoryId.ToString()));
            IBusinessTask businessTask = new BusinessTask();
            ITask task = businessTask.GetTask(int.Parse(taskId.ToString()));
            return string.Format("{0}/{1}", task.TaskShortName, userStory.UserStoryDescription);

        }
        protected string getTaskName(object taskId)
        {
            IBusinessTask businessTask = new BusinessTask();
            ITask task = businessTask.GetTask(int.Parse(taskId.ToString()));

            return string.Format("{0}-{1}", task.TaskShortName, task.Description);
        }

        protected string getTaskCompletedBy(object employeeId)
        {
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(int.Parse(employeeId.ToString()));
            return employee.NickName;
        }
        protected string getStatusName(object status)
        {
            IBusinessStatus businessStatus = new BusinessStatus();
            string StatusName = businessStatus.getStatusName(char.Parse(status.ToString()));
            return StatusName;
        }
        protected string getCodeReviewStatusName(object codeReviewStatus)
        {
            IBusinessCodeReviewStatus businessCodeReviewStatus = new BusinessCodeReviewStatus();
            string CodeReviewStatusName = businessCodeReviewStatus.getCodeReviewStatusName(char.Parse(codeReviewStatus.ToString()));
            return CodeReviewStatusName;
        }
        protected string getCodeReviewDoneByName(object employeeId)
        {
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(int.Parse(employeeId.ToString()));
            return employee.NickName;
        }


        protected void repeaterShowTechnicalTaskDetails_OnItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string technicalTaskId;
            string userStoryId;
            switch (e.CommandName)
            {
                case "EditTechnicalTask":
                    technicalTaskId = e.CommandArgument.ToString();

                    // IBusinessUserStory businessUserStory = new BusinessUserStory();
                    IBusinessTechnicalTask businessTechnicalTask = new BusinessTechnicalTask();
                    ITechnicalTask technicalTask = businessTechnicalTask.GetTechnicalTask(int.Parse(technicalTaskId));
                    userStoryId = technicalTask.UserStoryId.ToString();

                    ClientScript.RegisterStartupScript(this.GetType(), "UpdateTechnicalTask", "UpdateTechnicalTask('" + technicalTaskId +"','" + userStoryId + "');", true);
                    break;
               
            }

        }

        protected void repeaterShowUserStoryDetails_OnItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string userStoryId;
            switch (e.CommandName)
            {
                case "AddTechnicalTask":
                    userStoryId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "AddTechnicalTask", "AddTechnicalTask('" + userStoryId + "');", true);
                    break;
            }
        }
        protected string getEmpName(object employeeId)
        {


            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(int.Parse(employeeId.ToString()));

            return employee.NickName;
        }
        [System.Web.Services.WebMethod]
        public static int UpdateStatus(string taskId, string newStatus, string employeeId)
        {
            int retval = 0;
            IBusinessTechnicalTask businessTechnicalTask = new BusinessTechnicalTask();
            ITechnicalTask _technicalTask = businessTechnicalTask.GetTechnicalTask(int.Parse(taskId));

            if ((_technicalTask.TaskCompletedBy == null) || (_technicalTask.TaskCompletedBy == "0")
                || (_technicalTask.TaskCompletedBy == string.Empty)
                || (_technicalTask.TaskCompletedBy == employeeId))
            {
                //int taskId, string newStatus,int employeeId
                //IBusinessTechnicalTask businessTechnicalTask = new BusinessTechnicalTask();
                ITechnicalTask technicalTask = new TechnicalTask();
                technicalTask.TechnicalTaskId = int.Parse(taskId);
                technicalTask.Status = newStatus;
                technicalTask.TaskCompletedBy = employeeId.ToString();

                retval = businessTechnicalTask.UpdateTechTaskStatus(technicalTask);
            }
            else
            {
                retval = -99;
            }
            return retval;


        }
    }
}